/*	SYMBIOS
 * 	gesture_rec.cpp
 *  Created on: Jun 1, 2009
 *      Author: andrei
 */

#include <math.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include "gesture_rec.h"
#include "parameters.h"

IplImage* pInterface = 0;
CvPoint2D32f previousCenter;
const char * DISPLAY_WINDOW = "DisplayWindow";
CvRect rect1;
CvRect rect2;
CvRect rect3;
int selectedRect=-1;

void updateObjects( CvPoint previousCenter, int result);

void initInterface () {

	//FENETRES
	cvNamedWindow( DISPLAY_WINDOW, 1 );
	cvNamedWindow( "Interface", 1);
	cvMoveWindow( "Interface",0,0 );
	rect1.x = 70;
	rect1.y = 70;
	rect2.x = 120;
	rect2.y = 30;
	rect3.x = 100;
	rect3.y = 70;
	rect1.width = 40;
	rect1.height = 30;
	rect2.width = 50;
	rect2.height = 50;
	rect3.width = 20;
	rect3.height = 70;
	previousCenter.x=0.0;
	previousCenter.y=0.0;
	cvNamedWindow("test1",1);
	cvMoveWindow("test1",800,100);
	//cvNamedWindow("hand",1);
//	cvMoveWindow("hand",600,300);*/
}



void affichage(IplImage* pVideoFrame, CvPoint2D32f center , int result ){

	if( pInterface == 0 )
		pInterface = cvCreateImage( cvSize(640,400), 8 ,3) ;
	cvSet(pInterface, CV_RGB(255,255,255));

	CvPoint circleCenter;
	circleCenter.x = (int)(0.3* center.x + 0.7*previousCenter.x);
	circleCenter.y = (int)(0.3* center.y + 0.7*previousCenter.y);

	updateObjects(circleCenter, result);

	circleCenter.x = circleCenter.x *SYMB_RESIZE;
	circleCenter.y = circleCenter.y *SYMB_RESIZE;


	cvRectangle(pInterface,cvPoint((rect1.x - rect1.width/2)*SYMB_RESIZE,(rect1.y- rect1.height/2)*SYMB_RESIZE),cvPoint((rect1.x+rect1.width/2)*SYMB_RESIZE,(rect1.y+rect1.height/2)*SYMB_RESIZE), CV_RGB(0,255,0), CV_FILLED, 8, 0 );
	cvRectangle(pInterface,cvPoint((rect2.x - rect2.width/2)*SYMB_RESIZE,(rect2.y- rect2.height/2)*SYMB_RESIZE),cvPoint((rect2.x+rect2.width/2)*SYMB_RESIZE,(rect2.y+rect2.height/2)*SYMB_RESIZE), CV_RGB(0,0,255), CV_FILLED, 8, 0 );
	cvRectangle(pInterface,cvPoint((rect3.x - rect3.width/2)*SYMB_RESIZE,(rect3.y- rect3.height/2)*SYMB_RESIZE),cvPoint((rect3.x+rect3.width/2)*SYMB_RESIZE,(rect3.y+rect3.height/2)*SYMB_RESIZE), CV_RGB(255,0,0), CV_FILLED, 8, 0 );
	cvCircle(pInterface, circleCenter, 5.0, CV_RGB(0,0,0),CV_FILLED,8, 0 );

	cvFlip(pInterface,pInterface, 1);
	cvShowImage("Interface", pInterface);
	cvShowImage( DISPLAY_WINDOW, pVideoFrame );
}


void closeInterface() {
	//RELEASE WINDOW
	cvDestroyWindow( DISPLAY_WINDOW );
	cvDestroyWindow( DISPLAY_WINDOW);
	cvReleaseImage(&pInterface);
	cvDestroyWindow( "test1");
	cvDestroyWindow("hand");
}


void updateObjects( CvPoint center, int result){

	if (result == 1) {
		if (selectedRect == -1) {
			if (center.x < (rect1.x+ rect1.width/2) && center.x >(rect1.x-rect1.width/2) && center.y<(rect1.y+ rect1.height/2) && center.y >(rect1.x-rect1.height/2))
				selectedRect=1;
			else if (center.x < (rect2.x + rect2.width/2) && center.x >(rect2.x-rect2.width/2) && center.y<(rect2.y+ rect2.height/2) && center.y >(rect2.x-rect2.height/2))
				selectedRect=2;
			else if (center.x < (rect3.x+ rect3.width/2) && center.x >(rect3.x-rect3.width/2) && center.y<(rect3.y+ rect3.height/2) && center.y >(rect3.x-rect3.height/2))
				selectedRect=3;
			switch(selectedRect){
				case 1 : rect1.x = center.x;
						 rect1.y = center.y;
						 break;
				case 2 : rect2.x = center.x;
						 rect2.y = center.y;
						 break;
				case 3 : rect3.x = center.x;
						 rect3.y = center.y;
						 break;
					}
		} else
				selectedRect = -1;
	}
	else{
		switch(selectedRect){
						case 1 : rect1.x = center.x;
								 rect1.y = center.y;
								 break;
						case 2 : rect2.x = center.x;
								 rect2.y = center.y;
								 break;
						case 3 : rect3.x = center.x;
								 rect3.y = center.y;
								 break;
		}
	}
printf("%d\n", selectedRect);
}

